package org.soarsky.wealthchengdu.module.info;

import java.util.ArrayList;
import java.util.List;

import org.soarsky.wealthchengdu.R;
import org.soarsky.wealthchengdu.appliction.MyAppliction;
import org.soarsky.wealthchengdu.entity.FocusData;
import org.soarsky.wealthchengdu.entity.JsonResult;
import org.soarsky.wealthchengdu.entity.LogonResult;
import org.soarsky.wealthchengdu.entity.NewsData;
import org.soarsky.wealthchengdu.entity.NewsResultMsg;
import org.soarsky.wealthchengdu.module.focus.FocusActivity;
import org.soarsky.wealthchengdu.module.home.AllGalleryShow;
import org.soarsky.wealthchengdu.module.home.GalleryTaskTimer;
import org.soarsky.wealthchengdu.module.home.HomeActivty;
import org.soarsky.wealthchengdu.module.home.HomeDetailsActivity;
import org.soarsky.wealthchengdu.network.ParserResults;
import org.soarsky.wealthchengdu.util.Config;
import org.soarsky.wealthchengdu.view.ALLSendDialog;
import org.soarsky.wealthchengdu.view.AddView;
import org.soarsky.wealthchengdu.view.PullToRefreshView;
import org.soarsky.wealthchengdu.view.PullToRefreshView.OnFooterRefreshListener;
import org.soarsky.wealthchengdu.view.PullToRefreshView.OnHeaderRefreshListener;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 聚焦成都 ，世说成都等有listview得界面
 * 
 * @author huang
 * 
 */
public class FocusCDDataShow extends AddView {
	private ImageView[] imageview;// 小圆点
	private Gallery focuscdgllery;// 画廊
	private FoucsGalleryAdapter mGalleryAdapter; // 画廊适配器
	private FocusCDListAdapter focusCDListAdapter;// list适配器
	private ListView focuscdlist;// listview列表
	private LinearLayout linerlayout;// 装载小圆点容器
	private PullToRefreshView mPullToRefreshView;// listview拉动更多
	private static FocusCDDataShow focusCDDataShow = null;
	List<FocusData> tempImages;
	private int page = 1;
	List<String> galleryNumber;
	 public static  List<String> gtextnumber;
	private boolean flag = false;
    public static TextView gText;
	private List<NewsData> listviewdata;
	List<FocusData> focusdata;
	private ParserResults service;
	
	private static AsyncTask<Void, Void, Boolean> newsdata;
	private static AsyncTask<Void, Void, Boolean> focusimage;
	
	// gridview假数据
	int d_img[] = { R.drawable.home_img_park, R.drawable.home_img_city };

	public FocusCDDataShow(Activity activity) {
		super(activity);
		init();
	}

	public void init() {
		galleryNumber=new ArrayList<String>();
		gtextnumber=new ArrayList<String>();
		mGalleryAdapter = new FoucsGalleryAdapter(activity);
		service = new ParserResults();
		listviewdata = new ArrayList<NewsData>();
		focusCDListAdapter = new FocusCDListAdapter(activity);
	}

	/**
	 * 获取addview实例
	 * 
	 * @param context
	 * @return
	 */
	public static FocusCDDataShow getinstace(Activity activity) {
		if (focusCDDataShow == null) {
			focusCDDataShow = new FocusCDDataShow(activity);
		}
		return focusCDDataShow;
	}

	/**
	 * 获取控件
	 * 
	 * @param view
	 */
	public View getview() {

		View view = getinfalntview(R.layout.activity_foucs_cd);
		focuscdgllery = (Gallery) view.findViewById(R.id.activity_list_foucscd_gallery);
		focuscdgllery.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				 if(galleryNumber.size()>0){
					 Intent intent=new
					 Intent(activity,HomeDetailsActivity.class);
					 MyAppliction.ID=galleryNumber.get(arg2%galleryNumber.size());
					 Log.e("ss",MyAppliction.ID+ "!!!!!!!!!!!!!!!!!!!!!!!");
					 Log.e("ss", arg2+"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
					 intent.putExtra("gNumber",galleryNumber.get(arg2%gtextnumber.size()));
					 activity.startActivity(intent);
					 }
			}
		});
		
		focuscdlist = (ListView) view
				.findViewById(R.id.activity_list_foucscd_list);
		gText=(TextView) view.findViewById(R.id.acticity_list_fouces_gallerytext);
		linerlayout = (LinearLayout) view.findViewById(R.id.activity_list_gallerylinear);
		mPullToRefreshView = (PullToRefreshView) view
				.findViewById(R.id.main_pull_refresh_view);
		mPullToRefreshView
				.setOnHeaderRefreshListener(new this_OnHeaderRefreshListener());
		mPullToRefreshView
				.setOnFooterRefreshListener(new this_OnFooterRefreshListener());

		focuscdlist.setAdapter(focusCDListAdapter);
		// 画廊
		focuscdgllery.setAdapter(mGalleryAdapter);
		
		
		
		
		
		if (listviewdata.size() > 0) {
			listviewdata.clear();
			MyAppliction.list.clear();
		}
		focuscdlist.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(activity, FocusActivity.class);
				Bundle bundle = new Bundle();
				bundle.putInt("position", position);
				intent.putExtras(bundle);
				activity.startActivity(intent);
				MyAppliction.ID = listviewdata.get(position).getId();
			}
		});
		// 画廊数据
		tempImages = new ArrayList<FocusData>();
//		tempImages.add(d_img[0]);
//		tempImages.add(d_img[1]);
//		imageview = new ImageView[d_img.length];
//		mGalleryAdapter.setTempList(tempImages);
//
//		AllGalleryShow.getinstanise().setLinerNumber(focuscdgllery, activity,
//				d_img.length, linerlayout, R.drawable.dian2, R.drawable.dian1,
//				imageview);// 设置小圆点
//		GalleryTaskTimer.starttimer(focuscdgllery, linerlayout,
//				R.drawable.dian2, R.drawable.dian1, imageview);// 自动滑动

		loadDataNews();
		loadDatasFocus();
		return view;
	}
	
	
	
	private void loadDatasFocus() {
		Log.e("ss", "1111111111111111111111111111111");
//		progressDialog.setMessage("正在获取新闻列表信息...");
//		progressDialog.show();
		focusimage = new AsyncTask<Void, Void, Boolean>() {

			@Override
			protected Boolean doInBackground(Void... params) {
				// TODO Auto-generated method stub
				getLogon();
				try {
					Log.e("ss", "11111111111111111111111111111");
					//JsonResult<WeatherData> result = service.getWeather("0");
					JsonResult<List<FocusData>> focus = service.getFocus(MyAppliction.mChoose+"");
					Log.e("ss", "2222222222222222222222222222222222");
					
					if (focus == null){
						return false;}
					if (focus.isError()) {
						error(focus);
						// 这里做 数据错误处理
						return false;
					} else {
//						WeatherData dataMsg = result.getData();
						List<FocusData> fdata = focus.getData();
						Log.e("ss", "333333333333333333333333333333");
						if (fdata.size() > 0) {
							for (FocusData data : fdata) {
								tempImages.add(data);
								//galleryNumber.add(data.get(i).getId());
								//Log.e("ss", focusdata.get(1).getUrl()+"aaaaaasw");//日期
							}
						}
						Log.e("ss", "44444444444444444444444444444444");
						
						for(int i=0;i<tempImages.size();i++){
							//images.add(focusdata.get(i).getImg());
							galleryNumber.add(tempImages.get(i).getId());
							if(MyAppliction.mCn==0){
								gtextnumber.add(tempImages.get(i).getName());
							}else {
								gtextnumber.add(tempImages.get(i).getNameEn());
							}
						
							Log.e("ss", tempImages.get(i).getName()+"addaaaaasssssssssssss");
						}
//						images.clear();
//						for(int i=0;i<focusdata.size();i++){
//							images.add(focusdata.get(i).getImg());
//						}
//						
						Log.e("ss", "5555555555555555555555555");
						
						if (fdata!= null) {
						//	Log.e("ss", fdata.get(0).getImg()+ "wwwwwwwwwwwwwwwwwww");
//							Log.e("ss", focusdata.get(0).getImg()+"wwwwwwwwwwwwwwwwwww");
						}
						
						
						return true;
					}

				} catch (Exception e) {
					// TODO Auto-generated catch block
				}
				return false;
			}

			protected void onPostExecute(Boolean result) {
				// UI更新操作
				if (result) {
//					mGallaryAdapter.setList(focusdata);
					mGalleryAdapter.setTempList(tempImages);
					
					imageview = new ImageView[tempImages.size()];
					AllGalleryShow.getinstanise().setLinerNumber(focuscdgllery,
							activity, tempImages.size(), linerlayout,
							R.drawable.dot_active, R.drawable.dot_ico, imageview);// 设置小圆点
					GalleryTaskTimer.starttimer(focuscdgllery, linerlayout,
							R.drawable.dot_active, R.drawable.dot_ico, imageview);// 自动滑动
				} else {

				}
			//	progressDialog.dismiss();
			};
		}.execute();
	}
	
	
	

	/**
	 * 
	 * @ClassName: this_OnFooterRefreshListener
	 * @Description: 上拉加载监听器
	 * @author 吴荣
	 * @date 2013-4-24 上午10:29:16
	 * 
	 */
	class this_OnFooterRefreshListener implements OnFooterRefreshListener {

		@Override
		public void onFooterRefresh(PullToRefreshView view) {
			// mPullToRefreshView.postDelayed(new Runnable() {
			// @Override
			// public void run() {
			page++;
			loadDataNews();
			// }
			// }, 1000);

		}
	}
	
	
	
	
	
	

	/**
	 * 
	 * @ClassName: this_OnHeaderRefreshListener
	 * @Description: 下拉刷新监听器
	 * @author 吴荣
	 * @date 2013-4-24 上午10:29:16
	 * 
	 */
	class this_OnHeaderRefreshListener implements OnHeaderRefreshListener {

		@Override
		public void onHeaderRefresh(PullToRefreshView view) {
			// mPullToRefreshView.postDelayed(new Runnable() {

			// @Override
			// public void run() {
			page = 1;
			flag = true;
			loadDataNews();

			// }
			// }, 1000);

		}

	}
	
	
	
	
	
	
	

	private void loadDataNews() {
		newsdata = new AsyncTask<Void, Void, Boolean>() {

			@Override
			protected Boolean doInBackground(Void... params) {
				// TODO Auto-generated method stub
				getLogon();
				try {
					JsonResult<NewsResultMsg> result = service.getNews(
							MyAppliction.mcateID, "2", null, null,
							String.valueOf(page), null, null);
					//
					if (result == null)
						return false;
					if (result.isError()) {
						error(result);
						// 这里做 数据错误处理
						return false;
					} else {
						NewsResultMsg dataMsg = result.getData();
						List<NewsData> list = dataMsg.getData();
						if (list != null && list.size() != 0) {// 获取数据
							if (flag) {
								if (listviewdata.size() > 0) {
									listviewdata.clear();
									MyAppliction.list.clear();
								}
							}
							for (NewsData d : list) {
								listviewdata.add(d);
								Log.e("ID", d.getId()+"");
								MyAppliction.list.add(d.getId());
							}

							return true;
						}

					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
				}
				return false;
			}

			protected void onPostExecute(Boolean result) {
				// UI更新操作
				if (result) {
					focusCDListAdapter.setlistdata(listviewdata);
					mPullToRefreshView.onFooterRefreshComplete();
					mPullToRefreshView.onHeaderRefreshComplete("");// 更新时间,且隐藏头
				} else {
					Toast.makeText(
							activity,
							Config.changeLanguage(MyAppliction.mCn).getNodata(),
							Toast.LENGTH_LONG).show();
//					mPullToRefreshView.onFooterRefreshComplete();
//					mPullToRefreshView.onHeaderRefreshComplete("");// 更新时间,且隐藏头
				}
				ALLSendDialog.dimssdialog();
			};
		}.execute();
	}

	// 必须先登录
	private void getLogon() {
		try {
			JsonResult<LogonResult> jsonResult = service.authLogon("张三");
			if (jsonResult != null) {
				if (jsonResult.isError()) {
					error(jsonResult);
				} else {// 登录成功
					System.out.println(jsonResult.getData().getUserId() + "==="
							+ jsonResult.getData().getNickName());
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println(e.toString() + "===");
		}
	}

	private static void error(JsonResult<?> jsonResult) {
		System.out.println("错误码:" + jsonResult.getCode() + "===错误信息:"
				+ jsonResult.getMsg());
	}
	
	
	
}
